/**
 * 应用状态管理 - 主要状态store
 * 功能：管理应用的全局状态，包括聊天记录、搜索历史、用户设置等
 * 作者：AI Assistant
 * 创建时间：2025-01-15
 */
import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useAppStore = defineStore('app', () => {
  // 聊天相关状态
  const chatMessages = ref([])
  const isLoading = ref(false)
  const currentModel = ref('fast_thinking')
  
  // 搜索相关状态
  const searchHistory = ref([])
  const searchResults = ref([])
  const searchScope = ref('webpage')
  
  // 侧边栏状态
  const sidebarCollapsed = ref(false)
  const linkCards = ref([
    {
      id: 1,
      title: '官方文档',
      url: 'https://docs.example.com',
      description: '查看官方文档和API说明'
    },
    {
      id: 2,
      title: '使用指南',
      url: 'https://guide.example.com',
      description: '详细的使用指南和教程'
    }
  ])
  
  // 用户设置
  const userSettings = ref({
    theme: 'light',
    language: 'zh-CN',
    autoSave: true,
    notifications: true
  })
  
  // 计算属性
  const chatMessageCount = computed(() => chatMessages.value.length)
  const hasSearchResults = computed(() => searchResults.value.length > 0)
  const unreadCount = computed(() => {
    return chatMessages.value.filter(msg => !msg.read).length
  })
  const recentSearches = computed(() => {
    return searchHistory.value.slice(-5)
  })
  const currentTheme = computed(() => {
    return userSettings.value.theme
  })
  const isDarkTheme = computed(() => {
    return userSettings.value.theme === 'dark'
  })
  
  // 聊天相关方法
  function addChatMessage(message) {
    chatMessages.value.push({
      id: Date.now(),
      timestamp: new Date().toISOString(),
      ...message
    })
  }
  
  function clearChatMessages() {
    chatMessages.value = []
  }
  
  function setLoading(loading) {
    isLoading.value = loading
  }
  
  function setCurrentModel(model) {
    currentModel.value = model
  }
  
  // 搜索相关方法
  function addSearchHistory(query) {
    const historyItem = {
      id: Date.now(),
      query,
      timestamp: new Date().toISOString()
    }
    
    // 避免重复添加
    const exists = searchHistory.value.find(item => item.query === query)
    if (!exists) {
      searchHistory.value.unshift(historyItem)
      // 限制历史记录数量
      if (searchHistory.value.length > 50) {
        searchHistory.value = searchHistory.value.slice(0, 50)
      }
    }
  }
  
  function setSearchResults(results) {
    searchResults.value = results
  }
  
  function setSearchScope(scope) {
    searchScope.value = scope
  }
  
  function clearSearchHistory() {
    searchHistory.value = []
  }
  
  // 侧边栏相关方法
  function toggleSidebar() {
    sidebarCollapsed.value = !sidebarCollapsed.value
  }
  
  function addLinkCard(card) {
    linkCards.value.push({
      id: Date.now(),
      ...card
    })
  }
  
  function removeLinkCard(cardId) {
    const index = linkCards.value.findIndex(card => card.id === cardId)
    if (index > -1) {
      linkCards.value.splice(index, 1)
    }
  }
  
  // 用户设置相关方法
  function updateUserSettings(settings) {
    userSettings.value = { ...userSettings.value, ...settings }
  }
  
  function toggleTheme() {
    const newTheme = userSettings.value.theme === 'light' ? 'dark' : 'light'
    userSettings.value.theme = newTheme
    
    // 应用主题到DOM
    applyTheme(newTheme)
    
    // 保存到本地存储
    localStorage.setItem('theme', newTheme)
  }
  
  function setTheme(theme) {
    userSettings.value.theme = theme
    applyTheme(theme)
    localStorage.setItem('theme', theme)
  }
  
  function applyTheme(theme) {
    if (theme === 'dark') {
      document.documentElement.setAttribute('data-theme', 'dark')
    } else {
      document.documentElement.removeAttribute('data-theme')
    }
  }
  
  function initTheme() {
    const savedTheme = localStorage.getItem('theme') || 'light'
    setTheme(savedTheme)
  }
  
  return {
    // 状态
    chatMessages,
    isLoading,
    currentModel,
    searchHistory,
    searchResults,
    searchScope,
    sidebarCollapsed,
    linkCards,
    userSettings,
    
    // 计算属性
    chatMessageCount,
    hasSearchResults,
    unreadCount,
    recentSearches,
    currentTheme,
    isDarkTheme,
    
    // 方法
    addChatMessage,
    clearChatMessages,
    setLoading,
    setCurrentModel,
    addSearchHistory,
    setSearchResults,
    setSearchScope,
    clearSearchHistory,
    toggleSidebar,
    addLinkCard,
    removeLinkCard,
    updateUserSettings,
    toggleTheme,
    setTheme,
    applyTheme,
    initTheme
  }
})
